package com.cn.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.cn.dao.OrderDao;
import com.cn.domain.Order;
import com.cn.domain.OrderItem;
import com.cn.utils.JdbcUtils;

public class OrderDaoImpl implements OrderDao {

	private QueryRunner qr = new QueryRunner();
	
	public void saveOrder(Order o) {
		Connection conn = JdbcUtils.getConnectionWithTransaction();
		String sql = "INSERT INTO t_ORDER "+
                   "  (id,             "+
                   "  receiver,        "+
                   "  address,         "+
                   "  phone,           "+
                   "  statu,          "+
                   "  summoney,        "+
                   "  ordertime,       "+
                   "  uid,              "+
                   "  isdelete         "+
                   "  )                "+
                   "  VALUES           "+
                   "  (?,?,?,?,?,?,null,?,?);";             
             
		try {
			qr.update(conn, sql,o.getId(),o.getReceiver(),o.getAddress(),o.getPhone(),o.getStatu(),o.getSummoney(),o.getUid(),o.getIsdelete());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("添加订单失败！");
		}

	}

	public void saveOrderItem(OrderItem ot) {
		Connection conn = JdbcUtils.getConnectionWithTransaction();
		String sql = "INSERT INTO orderitem "+
					"  (oid,      "+
					"  pid,       "+
					"  buynum     "+
					"  )          "+
					"  VALUES    "+
					"  (?,?,?     "+
					"  );         ";    
		try {
			qr.update(conn, sql,ot.getOid(),ot.getPid(),ot.getBuynum());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("添加订单明细失败！");
		}

	}

	public List<Order> getOrderListByUserId(int id) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "select * from t_order where uid = ? and isdelete = 0";
		List<Order> list = new ArrayList<Order>();
		try {
			list =  qr.query(conn, sql, new BeanListHandler<Order>(Order.class),id);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据订单id查询列表订单失败！");
		}
		
	}

	public Order getOrderByOrderId(String id) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "select * from t_order where id = ? and isdelete = 0 ";
		Order o = new Order();
		try {
			o =  qr.query(conn, sql, new BeanHandler<Order>(Order.class),id);
			return o;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据订单id查询订单对象失败！");
		}
	}

	public List<OrderItem> getListOrderItemByOrderId(String id) {
		Connection conn = JdbcUtils.getConnection();
		String sql =" SELECT               "+
					"  p.name,             "+
					"  p.price,            "+
					"  oi.buynum           "+
					" FROM product p,       "+
					"  orderitem oi ,t_order    o   "+
					" WHERE oi.pid = p.id   "+
					"    AND oi.oid = ?   and  o.isdelete = 0 ";
		List<OrderItem> list = new ArrayList<OrderItem>();
		try {
			list =  qr.query(conn, sql, new BeanListHandler<OrderItem>(OrderItem.class),id);
			return  list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据订单id查询订单詳情失败！");
		}
	}

	public void deleteOrderByOrderId(String id) {
		Connection conn = JdbcUtils.getConnection();
		String sql = "update  t_order set isdelete = 1 where id = ?";
		try {
			qr.update(conn, sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("删除订单失败！");
		}
		
	}

	public void updateOrderStatu(String id) {
		Connection conn = JdbcUtils.getConnectionWithTransaction();
		String sql = "update  t_order set statu = 1 where id = ?";
		try {
			qr.update(conn, sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("修改支付状态失败！");
		}
		
	}

	public List<Order> getOrderlistByCondition(String oid, String receiver) {
		
		Connection conn = JdbcUtils.getConnection();
		
		String sql = "   SELECT                            "+
	                 "   o.id,                             "+
	                 "   o.receiver,                       "+
	                 "   o.address,                        "+
	                 "   o.phone,                          "+
	                 "   o.summoney,                       "+
	                 "   u.username,                       "+
	                 "   o.statu                           "+
	                 "  FROM t_order o                      "+
	                 "  INNER JOIN USER u ON u.id = o.uid  "+
	                 "  AND  o.isdelete = 0                  ";
		if(oid!=null && !"".equals(oid.trim())){
			System.out.println(oid);
			sql+=" and o.id = '"+oid+"' ";
		}
		if(receiver!=null && !"".equals(receiver.trim())){
			System.out.println(receiver);
			sql+=" and o.receiver = '"+receiver+"' ";
		}
		List<Order> list = null;
		try {
			list = qr.query(conn, sql, new BeanListHandler<Order>(Order.class));
			System.out.println(list);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("根据订单id和收件人查询订单列表失败！");
		}
	}
}
